<!DOCTYPE html>
<html class="no-js" lang="zh_CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">

    <title>${system_admin_title}</title>
    #required_css
</head>

<body>
<div class="app $!layout">
    <!-- 头部菜单 -->
    <section class="layout">
        <!-- 菜单 -->
        <section class="container">
            <!-- 内容主体 -->
            <section class="main-content">
                <!-- 主体操作栏 -->
                <header class="header navbar">
                    <!-- 左按钮区 -->
                    <div class="btn-group toolbar">
                        <a class="btn btn-primary toolbar-item" onclick="doAddRoute()"><i class="fa fa-plus"></i> 新增路由</a>
                    </div>
                    <div class="pull-right toolbar">
                        <a class="btn btn-primary toolbar-item" onclick="doSync()"><i class="ti-reload"></i> 同步缓存</a>
                    </div>
                </header>
                <div class="content-wrap">
                    <div class="wrapper">
                        <div class="row">
                            <div class="col-lg-12 col-md-12">
                                <table id="dgRoute"></table>
                                <div id="dgRoutePager"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <a class="exit-offscreen"></a>
            </section>
        </section>
    </section>
</div>
#required_js
<script type="text/javascript">
    ns.requireJS("/framework/js/view/datagrid.js");
    var dgRoute;
    ns.ready(function () {
        dgRoute = $("#dgRoute").jqGrid({
            url: '$!basePath/system/senior/route/data',
            colModel: [
                {label: "ID", name: "id", key: true, hidden: true},
                {label: "来源URL", name: "sourceUrl", width:"400"},
                {label: "转向URL", name: "targetUrl", width:"400"},
                {label: "重定向", name: "redirect", formatter:ns.view.datagrid.renderEnabled},
                {label: "内部链接", name: "internal", formatter:ns.view.datagrid.renderEnabled},
                {label: "状态", name: "enabled", formatter:ns.view.datagrid.renderEnabled},
                {
                    label: '操作', formatter: function (val, node, data) {
                    return ns.view.datagrid.renderOp([
                        {
                            title: "查看详情", onclick: function () {
                            ns.view.showModal("$!basePath/system/senior/route/detail/" + data.id);
                        }
                        },
                        {
                            title: "编辑", onclick: function () {
                            ns.view.showModal("$!basePath/system/senior/route/edit/" + data.id, {
                                onHidden: function (dialog, obj) {
                                    if (dialog.isEdited) {
                                        dgRoute.reload();
                                    }
                                }
                            });
                        }
                        },
                        {
                            title: "删除", onclick: function () {
                            doDeleteRoute(data.id);
                        }
                        },
                        {divider:true},
                        {
                            title: "启用", onclick: function () {
                            doEnable(data.id);
                        }
                        },
                        {
                            title: "禁用", onclick: function () {
                            doDisable(data.id);
                        }
                        }
                    ]);
                }
                }
            ],
            rownumbers: true,
            height: ns.view.datagrid.autoHeight(),
            pager: "#dgRoutePager",
            sortname: "createTime",
            sortorder: "desc"
        });
    });
    function doAddRoute() {
        ns.view.showModal("$!basePath/system/senior/route/add", {
            onHidden: function (dialog, obj) {
                if (dialog.isSaved) {
                    dgRoute.reload();
                }
            }
        });
    }
    function doDeleteRoute(id) {
        ns.tip.confirm("确定要删除此条数据吗？", function () {
            ns.get("$!basePath/system/senior/route/delete/" + id, function (success, data) {
                if (success) {
                    ns.tip.toast.success("数据已删除");
                    dgRoute.reload();
                } else {
                    ns.tip.toast.error(data.message);
                }
            });
        });
    }
    function doEnable(id){
        ns.get("$!basePath/system/senior/route/enable/"+id, function(success, data){
            if(success){
                ns.tip.toast.success("已启用，生效请同步缓存");
                dgRoute.reload();
            }else{
                ns.tip.toast.error(data.message);
            }
        });
    }
    function doDisable(id){
        ns.get("$!basePath/system/senior/route/disable/"+id, function(success, data){
            if(success){
                ns.tip.toast.success("已禁用，生效请同步缓存");
                dgRoute.reload();
            }else{
                ns.tip.toast.error(data.message);
            }
        });
    }
    function doSync(){
        ns.get("$!basePath/system/senior/route/sync", function(success, data){
            if(success){
                ns.tip.toast.success("缓存已同步");
            }else{
                ns.tip.toast.error(data.message);
            }
        });
    }
</script>
</body>
</html>